package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.MeterModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 电表业务
 * @author zhaokf
 * @date 2013-04-19
 */
public class MeterService extends BaseService
{
	/**
	 * 分页列出电表
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<MeterModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?10:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 10;
			page = 1;
		}
		StringBuffer where = new StringBuffer();
		try{
			if (search.get("mm_no") != null){
				where.append(" AND mm_no=" + search.get("mm_no"));
			}
			if (search.get("mm_name") != null){
				where.append(" AND mm_name LIKE '%" + search.get("mm_name") + "%'");
			}
		}catch (Exception e){
		}
		Page<MeterModel> MM = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM C_METER WHERE 1=1" + where;

		try{
			MM = MeterModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return MM;
	}
	
	/**
	 * 根据ID查询电表
	 * @param params
	 * @return
	 */
	public static MeterModel get(Map<String,String> params){
		String no = params.get("meter_no")==null?"":params.get("meter_no").toString();
		MeterModel pp = MeterModel.dao.findFirst("SELECT * FROM C_METER WHERE METER_NO=?", no);
		return pp;
	}




	/**
	 * 增加电表信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		Record record = new Record();
		record.set("METER_NO", Db.queryBigDecimal("select c_meter_seq.nextval from dual").intValue());
		record.set("METER_RATE", params.get("meter_rate").toString());
		record.set("METER_ORG_NO", params.get("meter_org_no").toString());
		boolean isupadded = Db.save("C_METER", "METER_NO",record);
		if(isupadded){
			return true;
		}
		return setError("保存电表信息失败！");
		
	}

	/**
	 * 修改电表信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String no = params.get("meter_no")==null?"":params.get("meter_no").toString();
		Record record = new Record();
		record.set("METER_NO", no);
		record.set("METER_RATE", params.get("meter_rate").toString());
		record.set("METER_ORG_NO", params.get("meter_org_no").toString());
		boolean isupadded = Db.update("C_METER", "METER_NO",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的电表点存在！");
	}

	/**
	 * 删除电表信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("meter_no")==null?"":params.get("meter_no").toString();
		boolean deleted = Db.deleteById("C_METER", "METER_NO", no);
		if (deleted){
			return true;
		}
		return setError("您访问的电表不存在！");
		
	}
}

